package com.xana.acg.mikomiko.frags.anime;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xana.acg.com.app.Fragment;
import com.xana.acg.com.data.DataSource;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.com.view.recycler.RecyclerV;
import com.xana.acg.fac.model.biliAnime.FilterItem;
import com.xana.acg.fac.model.biliAnime.FilterResp;
import com.xana.acg.fac.net.NetCallBack;
import com.xana.acg.fac.net.Network;
import com.xana.acg.mikomiko.App;
import com.xana.acg.mikomiko.R;

import net.qiujuer.genius.ui.Ui;

import java.util.List;

import butterknife.BindView;

public class AnimeFilterFragment extends Fragment implements DataSource.Callback<FilterResp> {
    @BindView(R.id.recycler)
    RecyclerV recycler;

    FilterAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.view_recycler;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        recycler.setBackgroundResource(R.drawable.bg_drawer);
        recycler.setTranslationY(Ui.dipToPx(getResources(), 50));
        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler.setAdapter(mAdapter = new FilterAdapter((Callback) getContext()));
    }

    @Override
    protected void initData() {
        super.initData();
        Network.biliAPI().getAnimeFilters().enqueue(new NetCallBack<>(this));
    }

    @Override
    public void ok(FilterResp resp) {
//        mAdapter.add(resp.filters);
        for (FilterItem filter : resp.filters) {
            if (!filter.title.equals("版权") && !filter.title.equals("付费"))
                mAdapter.add(filter);
        }
    }

    @Override
    public void fail(String msg) {
        App.showToast(msg);
    }

    static class FilterAdapter extends RecyclerAdapter<FilterItem> {
        static Callback callback;

        public FilterAdapter(Callback mcb) {
            callback = mcb;
        }

        @Override
        protected int getItemViewType(int position, FilterItem filterItem) {
            return R.layout.item_anime_filter;
        }

        @Override
        protected RecyclerAdapter.ViewHolder<FilterItem> onCreateViewHolder(View root, int viewType) {
            return new ViewHolder(root, callback);
        }

        static class ViewHolder extends RecyclerAdapter.ViewHolder<FilterItem> {
            @BindView(R.id.titleTextView)
            TextView title;
            @BindView(R.id.recycler)
            RecyclerV recycler;

            Callback callback;

            public ViewHolder(View itemView, Callback mcb) {
                super(itemView);
                callback = mcb;
            }

            @Override
            protected void onBind(FilterItem filterItem) {
                title.setText(filterItem.title);
                recycler.setLayoutManager(new GridLayoutManager(itemView.getContext(), 3));
                ListAdapter listAdapter = new ListAdapter(filterItem.list, new OnItemClickListener(getAdapterPosition()));
                recycler.setAdapter(listAdapter);
            }
        }

        static class OnItemClickListener implements RecyclerAdapter.AdapterListener<FilterItem.ListEntity> {
            private int index;

            public OnItemClickListener(int i) {
                index = i;
            }

            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, FilterItem.ListEntity listEntity) {
                TextView view = holder.itemView.findViewById(R.id.listText);
                view.setTextColor(0xBB03DAC5);
                callback.handle(index, listEntity.value);
            }

            @Override
            public void onItemLongClick(RecyclerAdapter.ViewHolder holder, FilterItem.ListEntity listEntity) {

            }
        }
    }

    static class ListAdapter extends RecyclerAdapter<FilterItem.ListEntity> {

        public ListAdapter(List<FilterItem.ListEntity> list, AdapterListener<FilterItem.ListEntity> listEntityAdapterListener) {
            super(list, listEntityAdapterListener);
        }

        @Override
        protected int getItemViewType(int position, FilterItem.ListEntity listEntity) {
            return R.layout.item_anime_filter_list;
        }

        @Override
        protected ViewHolder<FilterItem.ListEntity> onCreateViewHolder(View root, int viewType) {
            return new ViewHolder<FilterItem.ListEntity>(root) {
                TextView view;

                @Override
                protected void onBind(FilterItem.ListEntity listEntity) {
                    view = itemView.findViewById(R.id.listText);
                    view.setText(listEntity.name);
                    if (getAdapterPosition() == 0) view.setTextColor(0xBB03DAC5);
                }
            };
        }
    }

    public interface Callback {
        void handle(int keyIndex, String value);
    }

}